package com.springboot.frame.excel.handler;

import org.apache.poi.ss.usermodel.Cell;

/**
 * @author madman
 */
public class BooleanCellHandler extends AbstractBaseCellHandler<Boolean> {

  /**
   * Boolean类型处理器实例对象
   */
  static final BooleanCellHandler INSTANCE = new BooleanCellHandler();

  /**
   * Cell设值
   *
   * @param cell Cell对象
   * @param data 数据
   */
  @Override
  public void doWrite(Cell cell, Boolean data) {
    if (data != null) {
      cell.setCellValue(data ? "是" : "否");
    }
  }

  /**
   * Cell读值
   *
   * @param cell Cell对象
   * @return Cell对象上的值
   */
  @Override
  public Boolean read(Cell cell, Object object) {
    switch (cell.getCellTypeEnum()) {
      case STRING:
        String value = cell.getStringCellValue();
        return "是".equals(value) || "true".equalsIgnoreCase(value) || "y".equalsIgnoreCase(value)
            || "真".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value);
      case BOOLEAN:
        return cell.getBooleanCellValue();
      default:
        return null;
    }
  }

}
